package com.test.prolenders.util_prolenders;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.test.prolenders.base_prolenders.BaseApplication_prolenders;
import com.test.prolenders.config_prolenders.CountryConfig_prolenders;

/**
 * 用户信息管理
 * 初始化本地token/mobile等数据
 */
public class UserInfoUtil_prolenders {

    public static final String SP_KEY_TOKEN = "token";
    public static final String SP_KEY_MOBILE = "phoneNum";
    public static final String SP_KEY_USERID = "userid";
    public static final String SP_KEY_ISADSOURCE = "isAdSource";
    public static final String SP_KEY_AREACODE = "areaCode";

    public static final String SP_KEY_NORMALDIALOG_COUNT = "normal_dialog_count";
    public static final String SP_KEY_ACTIVEDIALOG_COUNT = "active_dialog_count";

    public static final String LOGIN_TYPE_REGISTER = "REGISTER";
    public static final String LOGIN_TYPE_OLD = "OLD";

    /**
     * 用户是否选择过额度
     */
    public static String USER_HAD_SELECTED_AMOUNT_FLAG = "NO";

    /**
     * 全局sp
     */
    private final SharedPreferences mSharedPreferences;

    /**
     * 登录手机号
     */
    private String mMobile;
    /**
     * 登录token
     */
    private String mToken;
    /**
     * 登录user_id
     */
    private String mUserId;
    /**
     * 是否为渠道用户
     */
    private boolean isAdSource;
    /**
     * 登录区号
     */
    private String areaCode;

    private int normaldialogCount;
    private int activedialogCount;

    private static class InnerUserInfoUtil {
        private static final UserInfoUtil_prolenders instance = new UserInfoUtil_prolenders();
    }

    public static UserInfoUtil_prolenders getInstance() {
        return InnerUserInfoUtil.instance;
    }

    private UserInfoUtil_prolenders() {
        mSharedPreferences = BaseApplication_prolenders.getInstant().getSharedPreferences(DecUtil_prolenders.getPackageName(), Context.MODE_PRIVATE);
        this.mMobile = mSharedPreferences.getString(SP_KEY_MOBILE, "");
        this.mToken = mSharedPreferences.getString(SP_KEY_TOKEN, "");
        this.mUserId = mSharedPreferences.getString(SP_KEY_USERID, "");
        this.isAdSource = mSharedPreferences.getBoolean(SP_KEY_ISADSOURCE,false);
        this.areaCode = mSharedPreferences.getString(SP_KEY_AREACODE,"");
        normaldialogCount = mSharedPreferences.getInt(SP_KEY_NORMALDIALOG_COUNT,0);
        activedialogCount = mSharedPreferences.getInt(SP_KEY_ACTIVEDIALOG_COUNT,0);
    }

    public void init(Context context) {}

    //判断是否已经登录
    public boolean didLogin() {
        return !TextUtils.isEmpty(this.mToken) && CountryConfig_prolenders.getInstance().getAreaCode().equalsIgnoreCase(areaCode);
    }

    public String getMobile() {
        if (TextUtils.isEmpty(mMobile)) mMobile="";
        return mMobile;
    }

    public String getEncryptMobile(){
        if (TextUtils.isEmpty(mMobile)) mMobile="";
        int length = mMobile.length();
        return "***"+mMobile.substring(length-4,length);
    }

    @SuppressLint("ApplySharedPref")
    public void setMobile(String mobile) {
        this.mMobile = mobile;
        mSharedPreferences.edit().putString(SP_KEY_MOBILE, this.mMobile).commit();
    }

    public String getToken() {
        if (TextUtils.isEmpty(mToken)) mToken = "";
        return mToken;
    }

    public String getUserId() {
        if (TextUtils.isEmpty(mUserId)) mUserId = "";
        return mUserId;
    }

    @SuppressLint("ApplySharedPref")
    public void setToken(String token) {
        this.mToken = token;
        mSharedPreferences.edit().putString(SP_KEY_TOKEN, this.mToken).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void setUserId(String userId) {
        this.mUserId = userId;
        mSharedPreferences.edit().putString(SP_KEY_USERID, this.mUserId).commit();
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
        mSharedPreferences.edit().putString(SP_KEY_AREACODE, this.areaCode).apply();
    }

    public boolean getIsAdSource(){
        //tag2024
        return true;
//        return isAdSource;
    }

    @SuppressLint("ApplySharedPref")
    public void setIsAdSource(boolean isAdSource){
        this.isAdSource = isAdSource;
        mSharedPreferences.edit().putBoolean(SP_KEY_ISADSOURCE,isAdSource).commit();
    }

    public int getNormaldialogCount() {
        return normaldialogCount;
    }

    public int getActivedialogCount() {
        return activedialogCount;
    }

    @SuppressLint("ApplySharedPref")
    public void addNormaldialogCount(){
        normaldialogCount++;
        mSharedPreferences.edit().putInt(SP_KEY_NORMALDIALOG_COUNT,normaldialogCount).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void addActivedialogCount(){
        activedialogCount++;
        mSharedPreferences.edit().putInt(SP_KEY_ACTIVEDIALOG_COUNT,activedialogCount).commit();
    }

    public void clear(){
        setMobile("");
        setUserId("");
        setToken("");
        setAreaCode("");
    }

}
